/*************************************************
 *FileName:      register.js
 *Description:   注册.js
 *Others:        陈灿
 *************************************************/
    /*
     * 启动
     */
    mui.init();
    mui('.mui-scroll-wrapper').scroll({
        indicators: false,      //是否显示滚动条 默认为true
        deceleration: 0.003,    //阻尼系数,系数越小滑动越灵敏 默认0.0006
        bounce: false           //是否启用回弹 默认true
    });

    var code_mobile = '';
    /*
     * 变量初始化
     */
    
    /*
     * 主程序
     */ 
    $(document).ready(function() 
    {
        //样式加载
        pagecss();
    });
    /**
     * pagecss
     * 样式加载
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function pagecss(){
        $('.register .sex a').on('tap',function(){
            $(this).addClass('checked').siblings().removeClass('checked');
        })
        //发送手机验证码
        $('.getcode').on('tap',function(){
            if($(this).hasClass('active')){
                var mobile = $("input[name='mobile']").val();
                code_mobile = mobile;
                if(mobile == '') {
                    $("input[name='mobile']").attr("placeholder", "请输入手机号！");
                    return false
                }
                if(mobile == '' || !/^1[3457896]\d{9}$/.test(mobile)) {
                    mui.toast('请输入正确的手机号码',{duration:2000,type:'div'});
                    return;
                }
                $(this).removeClass('active');
                var btnID = this;
                var times = 60;
                btnID.setAttribute("disabled","disabled");
                btnID.innerHTML = times+"s后重新发送";
                var outtime = setInterval(function(){
                    times--;
                    btnID.innerHTML = times+"s后重新发送";
                    if(times<=0){
                        $('.getcode').addClass('active')
                        clearInterval(outtime);
                        btnID.innerHTML = "发送验证码";
                        btnID.removeAttribute("disabled");
                    }
                },1000);
                $.ajax({
                    type: "post",
                    url: DOMAIN_NAME + "/index.php/api/code/registCode",
                    dataType:'jsonp',
                    data: { mobile: mobile },
                    success: function(data) {
                        if(data.error_code==0){
                            localStorage.replace_regist_code = data.code;
                        }else{
                            clearInterval(outtime);
                            $('.getcode').addClass('active');
                            btnID.innerHTML = "发送验证码";
                            btnID.removeAttribute("disabled");
                            mui.toast(data.error_msg)
                        }
                        $(this).addClass('active')
                        console.log(data)
                    }
                });
            }
        })
        $('.useragrre').on('tap',function(){
            $(this).toggleClass('active');
        })
        
        $('.useragrre-a').on('tap',function(event){
            common.pageTransition('useragrre.html')
            return false;
        })
        //注册
        $('.rdntn').on('tap',function(){
            $('.ys-input-list input').blur();
            var mobile = $("input[name='mobile']").val();
            var code = $("input[name='code']").val();
            var password = $("input[name='password1']").val();
            var password2 = $("input[name='password2']").val();
            var codes = localStorage.replace_regist_code;
            var sex = '';
            var ip=returnCitySN.cip;
            var reg = new RegExp(/[A-Za-z].*[0-9]|[0-9].*[A-Za-z]/);
            $('.sex > a').each(function(){
                if($(this).hasClass('checked')){
                    sex = $(this).attr('data-sex');
                }
            });
            if(mobile == '') {
                mui.toast('请输入手机号！',{ duration:2000, type:'div' });
                return false
            }else if(!/^1[3456789]\d{9}$/.test(mobile)) {
                $("input[name='mobile']").val('');
                mui.toast('手机号码不正确！',{ duration:2000, type:'div' });
                return false
            }else if(mobile != code_mobile) {
                $("input[name='mobile']").val('');
                mui.toast('手机号码必须填写获取验证码的号码！',{ duration:2000, type:'div' });
                return false
            }else if(code == '') {
                mui.toast('请输入验证码！',{ duration:2000, type:'div' });
                return false
            }else if(!codes){
                mui.toast('请先获取验证码！',{ duration:2000, type:'div' });
                return false
            }else if(password.length < 6) {
                mui.toast('密码不小于6位字符！',{ duration:2000, type:'div' });
                return false
            }else if(!reg.test(password)) {
                mui.toast('密码必须为数字和字母结合！',{ duration:2000, type:'div' });
                return false;
            }else if(password == '') {
                mui.toast('请输入密码！',{ duration:2000, type:'div' });
                return false
            }else if(password2 == '') {
                mui.toast('请输入确认密码！',{ duration:2000, type:'div' });
                return false
            }else if(password != password2) {
                $("input[name='password2']").val('');
                mui.toast('密码不一致！',{ duration:2000, type:'div' });
                return false
            }else if(!sex){
                mui.toast('请选择性别！',{ duration:2000, type:'div' });
                return false
            }else if(!$('.useragrre').hasClass('active')){
                mui.toast('请同意协议！',{ duration:2000, type:'div' });
                return false
            }
            $('body').append('<div class="ys-modal-loading"><div class="mui-spinner"></div></div>');
            $('body').on('tap','.ys-modal-loading',function(){
                $(this).remove();
            })
            $.ajax({
                type: "post",
                url: DOMAIN_NAME + "/index.php/api/user/regist",
                dataType:'jsonp',
                data: { mobile: mobile, code: code, password: password, codes: codes, sex: sex,ip:ip},
                success: function(data) {
                    if(data.error_code == 0) {
                        localStorage.replace_token = data.token;
                        localStorage.replace_uid = data.uid;
                        localStorage.replace_nickname = mobile;
                        localStorage.replace_mobile = mobile;
                        $(".ys-modal-loading").remove();
                        mui.alert('注册成功！', '提示', '确定', function() {
                            localStorage.removeItem('replace_regist_code');
                            $.ajax({
                                type: "post",
                                url: DOMAIN_NAME + "/index.php/api/user/login",
                                dataType:'jsonp',
                                data: { mobile: mobile, password: password},
                                success: function(data) {
                                    if(data.error_code == 0){
                                        if(data.gestures_status == '1'){
                                            localStorage.screens='true';
                                        }else{
                                            localStorage.screens='false';
                                        }
                                        localStorage.replace_token = data.access_token;
                                        localStorage.replace_uid = data.uid;
                                        localStorage.replace_nickname = data.nickname;
                                        localStorage.replace_mobile = data.mobile;
                                        localStorage.replace_face = data.face;
                                        localStorage.replace_sex = data.sex;
                                        localStorage.replace_sex = data.sex;
                                        localStorage.welcome='false';
                                        $(".ys-modal-loading").remove();
                                        mui.toast('登录成功！',{ duration:2000, type:'div' });
                                        setTimeout(function(){
                                            location.href='index.html';
                                        },500);
                                    }else{
                                        $(".ys-modal-loading").remove();
                                        mui.toast(data.error_msg,{ duration:2000, type:'div' });
                                    }
                                    
                                }
                            });
                        }, 'div');
                        
                    }else{
                        $(".ys-modal-loading").remove();
                        mui.alert(data.error_msg, '提示', '确定', function() {

                        }, 'div');
                    }
                }
            });
        })
  }
    
   
    
    /**
     * adds //方法名
     * @陈灿 //作者名
     * @DateTime 2018-01-010T0116:25:00+0800 //时间
     * @param    int    $goods_id //参数
     * @param    array  $data //参数
     * @return    array  [error_code, error_msg, goods_id] //返回值
     */


